home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT1-5 / BSORT.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-08-28  |  4.8 KB  |  113 lines

  1. ;*********************************************************************
  2. ;
  3. ;  Program BSort ( Chapter 4 )
  4. ;
  5. ;  Character arrays sorting. Version 1.4
  6. ;
  7. ;  Author: A.I.Sopin, Voronezh University, 16/03/93 1993
  8. ;
  9. ;  Using from assembler programs:
  10. ;
  11. ;  Call  BSORT
  12. ;
  13. ;  Modified bubblesort algorithm is used
  14. ;
  15. ;  Parameters passed
  16. ;
  17. ;  Entry parameters:
  18. ;  -----------------
  19. ;
  20. ;  DS:SI - address of an array to be sorted
  21. ;  CX    - the array element length
  22. ;  DX    - number of elements
  23. ;
  24. ;  Sorting order
  25. ;
  26. ;  AX = 0  - increasing
  27. ;       1  - decreasing
  28. ;
  29. ;  Output (error code in the AX register)
  30. ;  --------------------------------------
  31. ;  AX = 0 - normal finish
  32. ;       1 - elements longer than 48 bytes
  33. ;       2 - invalid parameters specified
  34. ;
  35. ;  The source array left unchanged
  36. ;
  37. ;********************************************************************
  38. .model       large
  39.    public    bsort
  40. MaxLen       equ       48                   ; maximum record length
  41. .code
  42. BSORT   PROC    FAR pascal uses bx cx dx es si di bp
  43. ;----------------------------------------------------------
  44. ;  Check parameters passed
  45.         cmp     cx,MaxLen       ;  array length valid?
  46.         jng     ChkN            ;  less than maximum - continue
  47.         mov     ax,1            ;  indicate "elements too long"
  48.         jmp     Exit            ;  return
  49. ChkN:   cmp     dx,0            ;  number of elements = 0?
  50.         jg      Work            ;  if not continue
  51.         mov     ax,2            ;  indicate "no elements in array"
  52.         jmp     Exit            ;  return
  53. ;----------------------------------------------------------
  54. Work:   mov     CS:SrtOrd,ax    ;  store Sort Order
  55.         cld                     ;  Clear Direct Flag
  56.         mov     CS:RecLen,cx    ;  store Record Length
  57.         mov     CS:RecNum,dx    ;  store Number Of Records
  58.         mov     CS:AddrArr,si   ;  store Array Address
  59.         push    ds              ;  push DARA segment addres
  60.         pop     es              ;  ES now points to DATA segment
  61. ;  External loop (WHILE SWP = 1, i.e while elements moved)
  62. ExtLoop:mov     CS:SWP,0        ;  indicate "no elements moved"
  63.         mov     ax,0            ;  Normal Return Code
  64.         dec     CS:RecNum       ;  decrease number of records to process
  65.         jz      Exit            ;  if no more records - exit
  66.         mov     dx,CS:RecNum    ;  of DX - number records left
  67.         mov     bp,CS:AddrArr   ;  starting address of array
  68.         mov     CS:BP0,bp       ;  save address of first record
  69. ;  Nested loop (comparing adjacent records)
  70. M1:     mov     si,CS:BP0       ;  address of current record
  71.         mov     di,si           ;  address of following record
  72.         mov     cx,CS:RecLen    ;  record length
  73.         add     di,cx           ;  address of next record
  74.         cmp     CS:SrtOrd,0     ;  increasing?
  75.         jnz     DecrS           ;  AX <> 0 - decreasing
  76. IncrS:  repe    cmpsb           ;  compare adjacent records
  77.         jbe     Swapped         ;  if following < current
  78.         jmp     short SWAP      ;  swap records
  79. DecrS:  repe    cmpsb           ;  compare adjacent records
  80.         jae     Swapped         ;  if following > current
  81. ;  Swapping records
  82. SWAP:   mov     cx,CS:RecLen    ;  CX - record length
  83.         mov     bp,CS:BP0       ;  BP - address of current record
  84.         mov     bx,bp           ;  BX points to current record
  85.         add     bx,cx           ;  now BX points to next record
  86. SwBytes:mov     al,DS:[BP]      ;  byte from current record into AL
  87.         xchg    al,[bx]         ;  swap bytes in AL and next record
  88.         mov     DS:[BP],al      ;  put byte into next record
  89.         inc     bp              ;  next byte in current record
  90.         inc     bx              ;  next byte in next record
  91.         loop    SwBytes         ;  repeat swapping
  92.         mov     CS:SWP,1        ;  indicate swappingg
  93. ;  advance parameters of external cycle
  94. Swapped:mov     ax,CS:RecLen    ;  AX - record length
  95.         add     CS:BP0,ax       ;  BP points to next record
  96.         dec     dx              ;  decrease number of records to process
  97.         jnz     M1              ;  next step of nested cycle
  98.         cmp     CS:SWP,0        ;  have elements been swapped?
  99.         jnz     ExtLoop         ;  yes - repeat process
  100.         mov     ax,0            ;  Normal Return Code
  101. ;  Restore registers and return (exit code in AX)
  102. Exit:   ret
  103. BSORT   ENDP
  104. ;----------------------------------------------------------
  105. ;  Data area in CODE segments
  106. SrtOrd  DW      0                       ;
  107. RecLen  DW      0                       ;
  108. RecNum  DW      0                       ;
  109. AddrArr DW      0                       ;
  110. BP0     DW      0                       ;  address of current record
  111. SWP     DB      0                       ;  swappings indicator
  112.         END
  113.